package com.yoomiito.app.ui.order.sureorder.singleGoods

import android.text.TextUtils
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.alibaba.fastjson.JSONObject
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.bean.AddressInfo
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.model.bean.UserMoneyInfo
import com.yoomiito.app.model.colleage.SubjectDetailInfo
import com.yoomiito.app.model.colleage.SubjectTableInfo
import com.yoomiito.app.model.my.MyRemainMoneyInfo
import com.yoomiito.app.model.order.CreateOrderInfo
import com.yoomiito.app.model.order.OrderingInfo
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.utils.YMToast
import io.reactivex.Flowable
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function
import okhttp3.RequestBody
import org.reactivestreams.Publisher
import java.util.LinkedHashMap

/**
 * 仅用于星店确认订单
 */
class SureOrderPresenter(appConfig: AppConfig) : BaseActPresenter<SureOrderActivity>(appConfig) {

    fun createOrder(productId: String, consignee: String, provinceId: String,
                    cityId: String, countryId: String, areaName: String, tel: String, address: String, note: String, num: String) {
        val params = LinkedHashMap<String, String>()
        params["product_id"] = productId
        params["consignee"] = consignee
        params["area_id"] = provinceId
        params["area_id2"] = cityId
        params["area_id3"] = countryId
        params["area_name"] = areaName
        params["mobile"] = tel
        params["address"] = address
        params["note"] = note
        params["num"] = num
        val body = RequestBody.create(YmApi.JSON, JSONObject.toJSONString(params))
//        Flowable.zip(YmApi.getYmService().createOrder(body), YmApi.getYmService().starStorePayInfo,
//                BiFunction<BaseRepository<CreateOrderInfo>, BaseRepository<UserMoneyInfo>, OrderingInfo> { orderInfo, moneyInfo ->
//                    OrderingInfo(orderInfo.data, moneyInfo.data)
//                })
//                .compose(v.bindToLifecycle())
//                .compose(XApi.getScheduler())
//                .doOnError {
//                    v.dismissLoading()
//                    YMToast.showText("请重试")
//                }
//                .subscribe { info ->
//                    v.pay(info)
//                }
        YmApi.getYmService().createOrder(body)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<CreateOrderInfo>(handleException()){
                    override fun onSuccess(t: CreateOrderInfo?) {
                        getPayInfo(t!!)
                    }

                    override fun onFail(error: ApiException?) {
                        if(hasV()){
                            v.dismissLoading()
                        }
                    }

                })
    }

    private fun getPayInfo(info:CreateOrderInfo){
        YmApi.getYmService().starStorePayInfo
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<UserMoneyInfo>(handleException()){
                    override fun onSuccess(t: UserMoneyInfo?) {
                        if (hasV()){
                            v.pay(OrderingInfo(info, t))
                        }
                    }

                    override fun onFail(error: ApiException?) {
                        if (hasV()){
                            v.dismissLoading()
                        }
                    }
                })
    }

    fun getAddress() {
        YmApi.getYmService()
                .defaultAddress
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<AddressInfo>() {
                    override fun onSuccess(addressInfo: AddressInfo) {
                        //                        if (addressInfos.size()==0){
                        v.showAddressView(addressInfo)
                        //                        }else {
                        //                            for (AddressInfo addressInfo:addressInfos) {
                        //                                if ("1".equals(addressInfo.getIsDefault())){
                        //                                    getV().showAddressView(addressInfo);
                        //                                }
                        //                            }
                        //                        }
                    }

                    override fun onFail(error: ApiException) {
                        v.dismissLoading()
                        if (!TextUtils.isEmpty(error.msg)) {
                            YMToast.showText(error.msg)
                        }
                    }
                })
    }
}